home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap07 / BlokOut2 / BlokOut2.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.7 KB  |  158 lines

  1. /*---------------------------------------------------
  2.    BLOKOUT2.C -- Mouse Button & Capture Demo Program
  3.                  (c) Charles Petzold, 1998
  4.   ---------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.                     PSTR szCmdLine, int iCmdShow)
  12. {
  13.      static TCHAR szAppName[] = TEXT ("BlokOut2") ;
  14.      HWND         hwnd ;
  15.      MSG          msg ;
  16.      WNDCLASS     wndclass ;
  17.  
  18.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  19.      wndclass.lpfnWndProc   = WndProc ;
  20.      wndclass.cbClsExtra    = 0 ;
  21.      wndclass.cbWndExtra    = 0 ;
  22.      wndclass.hInstance     = hInstance ;
  23.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  24.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  25.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  26.      wndclass.lpszMenuName  = NULL ;
  27.      wndclass.lpszClassName = szAppName ;
  28.      
  29.      if (!RegisterClass (&wndclass))
  30.      {
  31.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  32.                       szAppName, MB_ICONERROR) ;
  33.           return 0 ;
  34.      }
  35.      
  36.      hwnd = CreateWindow (szAppName, TEXT ("Mouse Button & Capture Demo"),
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.      
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.      
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.      {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.      }
  50.      return msg.wParam ;
  51. }
  52.  
  53. void DrawBoxOutline (HWND hwnd, POINT ptBeg, POINT ptEnd)
  54. {
  55.      HDC hdc ;
  56.      
  57.      hdc = GetDC (hwnd) ;
  58.      
  59.      SetROP2 (hdc, R2_NOT) ;
  60.      SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
  61.      Rectangle (hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y) ;
  62.      
  63.      ReleaseDC (hwnd, hdc) ;
  64. }
  65.  
  66. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  67. {
  68.      static BOOL  fBlocking, fValidBox ;
  69.      static POINT ptBeg, ptEnd, ptBoxBeg, ptBoxEnd ;
  70.      HDC          hdc ;
  71.      PAINTSTRUCT  ps ;
  72.      
  73.      switch (message)
  74.      {
  75.      case WM_LBUTTONDOWN :
  76.           ptBeg.x = ptEnd.x = LOWORD (lParam) ;
  77.           ptBeg.y = ptEnd.y = HIWORD (lParam) ;
  78.           
  79.           DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
  80.           
  81.           SetCapture (hwnd) ;
  82.           SetCursor (LoadCursor (NULL, IDC_CROSS)) ;
  83.           
  84.           fBlocking = TRUE ;
  85.           return 0 ;
  86.           
  87.      case WM_MOUSEMOVE :
  88.           if (fBlocking)
  89.           {
  90.                SetCursor (LoadCursor (NULL, IDC_CROSS)) ;
  91.                
  92.                DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
  93.                
  94.                ptEnd.x = LOWORD (lParam) ;
  95.                ptEnd.y = HIWORD (lParam) ;
  96.                
  97.                DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
  98.           }
  99.           return 0 ;
  100.           
  101.      case WM_LBUTTONUP :
  102.           if (fBlocking)
  103.           {
  104.                DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
  105.                
  106.                ptBoxBeg   = ptBeg ;
  107.                ptBoxEnd.x = LOWORD (lParam) ;
  108.                ptBoxEnd.y = HIWORD (lParam) ;
  109.                
  110.                ReleaseCapture () ;
  111.                SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
  112.                
  113.                fBlocking = FALSE ;
  114.                fValidBox  = TRUE ;
  115.                
  116.                InvalidateRect (hwnd, NULL, TRUE) ;
  117.           }
  118.           return 0 ;
  119.           
  120.      case WM_CHAR :
  121.           if (fBlocking & (wParam == '\x1B'))     // i.e., Escape
  122.           {
  123.                DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
  124.  
  125.                ReleaseCapture () ;
  126.                SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
  127.                
  128.                fBlocking = FALSE ;
  129.           }
  130.           return 0 ;
  131.           
  132.      case WM_PAINT :
  133.           hdc = BeginPaint (hwnd, &ps) ;
  134.           
  135.           if (fValidBox)
  136.           {
  137.                SelectObject (hdc, GetStockObject (BLACK_BRUSH)) ;
  138.                Rectangle (hdc, ptBoxBeg.x, ptBoxBeg.y,
  139.                                ptBoxEnd.x, ptBoxEnd.y) ;
  140.           }
  141.           
  142.           if (fBlocking)
  143.           {
  144.                SetROP2 (hdc, R2_NOT) ;
  145.                SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
  146.                Rectangle (hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y) ;
  147.           }
  148.           
  149.           EndPaint (hwnd, &ps) ;
  150.           return 0 ;
  151.           
  152.      case WM_DESTROY :
  153.           PostQuitMessage (0) ;
  154.           return 0 ;
  155.      }
  156.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  157. }
  158.